<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    //判断当前浏览器是否支持socket
    if(!window.WebSocket){
        alert("当前浏览器不支持webSocket")
    }else {
        socket = new WebSocket("ws://localhost:7000/hello");

        //相当于channelRead0,ev收到服务器端回送的消息
        socket.onmessage = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value=rt.value+"\n"+ev.data;
        }
        socket.onopen = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value="连接开启了"
        }

        socket.onclose = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value=rt.value+"\n"+"?连接关闭了"
        }
    }

    //发送消息到服务器
    function send(message) {
        if(!window.socket){
            return;
        }
        if(socket.readyState == WebSocket.OPEN){
            socket.send(message)
        }else {
            alert("连接没有开启")
        }
    }

</script>

    <form onsubmit="return false">
        <textarea name="message" style="height:300px; width: 300px"></textarea>
        <input type="button" value="发送消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px";width="300px"></textarea>
        <input type="button" value="清空消息" onclick="document.getElementById('responseText').value=''">
    </form>
</body>
</html>